#pragma once

#include<iostream>
#include <string>
#include <unordered_map>
#include<vector>
#include<cassert>
#include<fstream>
#include"../comm/log.hpp"
#include"../comm/util.hpp"




namespace ns_oj_model 
{
    using namespace ns_log;
    using namespace ns_util;
    using namespace std;

    class Model 
    {
    public:
        struct Question
        {
            string number;//题目编号
            string title;
            string star;//难度，简单，中等，困难
            int cpu_limit;//时间限制
            int mem_limit;//内存限制
            string desc;//题目内容
            string header;//题目框架
            string tail;//测试用例
        };

        const string questions_list="./questions/questions.list";
        const std::string questions_path = "./questions/";

        Model()
        {
            assert(LoadQuestionList(questions_list));
        }

        bool LoadQuestionList(const string& questions_list)//加载配置文件
        {
            ifstream in(questions_list);
            if(!in.is_open())
            {
                //Log1
                LOG(FATAL)<<"打开题库失败,请检查是否存在题库"<<"\n";
                return false;
            }

            //string path=questions_list;//////!
            string line;
            while(getline(in,line))
            {
                vector<string> tokens;//存储分割后的题目信息
                StringUtil::SplitString(line, &tokens, " ");
                //1 判断回文数 简单 1 3000
                if(tokens.size()!=5)
                {
                    //Log2
                    LOG(WARNING) << "加载部分题目失败, 请检查文件格式" << "\n";
                    continue;
                }
                Question q;
                q.number=tokens[0];
                q.title=tokens[1];
                q.star=tokens[2];
                q.cpu_limit=atoi(tokens[3].c_str());
                q.mem_limit=atoi(tokens[4].c_str());
                
                string path=questions_path;
                path+=q.number;
                path+="/";
                Oper_file::ReadFile(path+"desc.txt",&(q.desc) ,true);//
                Oper_file::ReadFile(path+"header.cpp",&(q.header) ,true);
                Oper_file::ReadFile(path+"tail.cpp",&(q.tail) ,true);
                questions.insert({q.number,q});//映射关系

            }

            in.close();
            LOG(INFO) << "加载题库成功!" << "\n";
            return true;
        }

        bool GetAllQuestions(vector<Question> *out)//?
        {
            if(questions.size()==0)
            {
                 LOG(ERROR) << "用户获取题库失败" << "\n";
                return false;
            }
            for(auto& q:questions)
            {
                (out)->push_back(q.second);
            }
            return true;
        }

        bool GetOneQuestion(const string& number,Question* out)
        {
            //iterator:first~key;second~value
            const auto iterator=questions.find(number);
            if(iterator==questions.end())
            {
                LOG(ERROR) << "用户获取题目失败, 题目编号: " << number << "\n";
                return false;
            }
            (*out)=iterator->second;
            return true;
        }
        ~Model()
        {}


    private:
        //建立题目编号与题目内容的映射；
        unordered_map<string, Question> questions;

    };

} // namespace ns_oj_model